﻿using MKAdminCore.DTO.Web.Common;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace MKAdminCore.DTO.CusAttribute
{
    public static class ModelPropertyAttribute
    {
        public static HttpResponseResult Validate<T>(this T dto)
        {
            var result = new HttpResponseResult();

            Type type = dto.GetType();
            //获取属性信息
            PropertyInfo[] properties = type.GetProperties();

            //获取指定字段值 
            //var property = type.GetProperty("OperatorPwd");
            //object pwdValue = property.GetValue(dto);

            foreach (var prop in properties)
            {
                //检查属性是否含有指定特性
                if (prop.IsDefined(typeof(AbstractCusAttribute), true))
                {
                    //获取属性值
                    //object oValue = prop.GetValue(dto);

                    //获取属性上的自定义特性
                    //AbstractCusAttribute cusAttribute = (AbstractCusAttribute)prop.GetCustomAttribute(typeof(AbstractCusAttribute));

                    //获取多个自定义特性
                    AbstractCusAttribute[] cusAttributes = (AbstractCusAttribute[])prop.GetCustomAttributes(typeof(AbstractCusAttribute));

                    foreach (var cusAttribute in cusAttributes)
                    {
                        result = cusAttribute.Validate(dto, prop);
                        if (result.Code != 200)
                        {
                            break;
                        }
                    }
                    if (result.Code != 200)
                    {
                        break;
                    }
                }
            }

            return result;
        }
    }
}
